/**
 * @author lk
 * @date 2022/6/6 13:52
 */
package com.mmall.thread;

public class WaitTest01 {
    public static void main(String[] args) {

        PrintNum printNum = new PrintNum();
        for (int i = 0; i < 2; i++) {
            Thread thread = new Thread(printNum);
            thread.setName(""+i);
            thread.start();
        }
        Runtime.getRuntime().addShutdownHook(new Thread(()-> System.out.println("nihao")));
    }
}

class PrintNum implements Runnable {
    int i = 1;

    @Override
    public void run() {
        synchronized (this) {
            printI();
        }
    }

    private void myWait() {
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void printI() {
        while (true) {
            notify();
            System.out.println(Thread.currentThread().getName() + ":" + i);
            i += 1;
            if (i > 100) {
                break;
            }
            myWait();
        }
    }
}
